﻿$(document).ready(function () {
    $(".concent").delegate("a", "click",
            function () {
                if ($(this).attr("isleaf") == 'True') {
                    PreviewsList.PageIndex = 1;
                    PreviewsList.GetData(this.id);
                }
            }
        );
});

var PreviewsList = {
    PageIndex: 1,
    PageSize: 10,
    PageCount: 1,
    GetData: function (ChapterId) {
        $(".bufen").remove();
        $(".title").after("<div class='bufen' style='text-align:center;'><img src='img/load.gif'></img></div>");

        $.ajax({
            type: "POST",
            contentType: "application/json; charset=UTF-8",
            url: "Student/Previews.aspx/GetPriviewList",
            data: "{ChapterId:" + ChapterId + ",iPageIndex:" + PreviewsList.PageIndex + ",iPageSize:" + PreviewsList.PageSize + "}",
            dataType: "json",
            success: function (obj) {

                var data = eval('(' + obj.d + ')');
                PreviewsList.PageCount = data.PageCount == 0 ? 1 : data.PageCount;
                if (!data.IsSuccess) {
                    alert(data.Msg);
                    if (data.Msg.indexOf("您已经登录超时") != -1) window.location.href = "/Login.aspx";
                } else {
                    var table = data.Table.TS_Preview_List;
                    var listHtml = "";
                    if (table.length == 0) {
                        listHtml += "<div class='bufen' id='nodata'>该知识点目前还没有前概念问题!</div>";
                    } else {
                        $(table).each(function (i, n) {
                            listHtml += "<div class=' bufen' style='border-top: 0'>";
                            listHtml += "<table border='0' cellspacing='0' cellpadding='0' width='100%'>";
                            listHtml += "<tr><td class='fontcolor' valign='top'>";
                            listHtml += "<a href='javascript:void(0);'>" + n.Question + "</a></td>";
                            listHtml += "<td valign='top' width='125px;' rowspan='2'><div class='" + (n.IsAnswer > 0 ? "unfinish" : "letter") + "'>";
                            listHtml += "<a href='/Student/Preview_Ask.aspx?p=" + n.PreviewId + "&i=" + PreviewsList.PageIndex + "&c=" + ChapterId + "'>" + (n.IsAnswer > 0 ? "我已回答" : "我要回答") + "</a></div></td></tr>";
                            listHtml += "<tr><td><table border='0' cellspacing='0' cellpadding='0' width='100%'>";
                            listHtml += "<tr><td valign='top'><div class='floatleft' style='padding-right: 10px;'>";
                            listHtml += "提问者：" + n.Name + "老师[ " + n.SchoolName + " ]</div>";
                            listHtml += "<div>" + n.PublishSmallDate + "发布</div>";
                            listHtml += "</td></tr></table></td></tr></table></div>";
                        });
                    }
                    $(".bufen").remove();
                    $(".title").after(listHtml);
                    $('.bufen:first').css("padding-top", "10px;");
                    PreviewsList.BuildPage();
                }
            },
            error: function (r, t, e) {
                alert(t);
            }
        });
    },
    BuildPage: function () {

        var prevPageClick = PreviewsList.PageIndex == 1 ? "javascript:void(0);" : "PreviewsList.JumpPage(" + (PreviewsList.PageIndex - 1) + ");";
        var nextPageClick = PreviewsList.PageIndex == PreviewsList.PageCount ? "javascript:void(0);" : "PreviewsList.JumpPage(" + (PreviewsList.PageIndex + 1) + ")";
        var pageHtml = PreviewsList.PageIndex == 1 ? "" : ("<td class='page' style='width: 40px;'><a href='javascript:void(0);' onclick='" + prevPageClick + "'>上一页</a></td>");
        var perPage = 5;
        var startPage = (PreviewsList.PageIndex - Math.floor(perPage / 2)) > 0 ? PreviewsList.PageIndex - Math.floor(perPage / 2) : 1;
        var endPage = (PreviewsList.PageIndex + Math.floor(perPage / 2)) > PreviewsList.PageCount ? PreviewsList.PageCount : PreviewsList.PageIndex + Math.floor(perPage / 2);
        var PageCurrentNumber = PreviewsList.PageIndex;

        if (PreviewsList.PageCount <= 9) {
            for (var i = 1; i <= PreviewsList.PageCount; i++) {
                if (i != PreviewsList.PageIndex) {
                    pageHtml += "<td class='page'><a href=\"javascript:void(0);\" onclick=\"PreviewsList.JumpPage(" + i + ");\">" + i + "</a></td>";
                } else {
                    pageHtml += "<td class='page'><a href=\"javascript:void(0);\" class=\"current\">" + i + "</a></td>";
                }
            }
        }
        else {
            if (PreviewsList.PageIndex > 3) {
                if (PreviewsList.PageIndex == 4) {
                    pageHtml += "<td class='page'><a href=\"javascript:void(0);\" onclick=\"PreviewsList.JumpPage(" + 1 + ");\" class=\"" + (PreviewsList.PageIndex == 1 ? "current" : "") + "\">" + 1 + "</a></td>";
                }
                else if (PreviewsList.PageIndex == 5) {
                    pageHtml += "<td class='page'><a href=\"javascript:void(0);\" onclick=\"PreviewsList.JumpPage(" + 1 + ");\" class=\"" + (PreviewsList.PageIndex == 1 ? "current" : "") + "\">" + 1 + "</a></td>";
                    pageHtml += "<td class='page'><a href=\"javascript:void(0);\" onclick=\"PreviewsList.JumpPage(" + 2 + ");\" class=\"" + (PreviewsList.PageIndex == 2 ? "current" : "") + "\">" + 2 + "</a></td>";
                }
                else if (PreviewsList.PageIndex == 6) {
                    pageHtml += "<td class='page'><a href=\"javascript:void(0);\" onclick=\"PreviewsList.JumpPage(" + 1 + ");\" class=\"" + (PreviewsList.PageIndex == 1 ? "current" : "") + "\">" + 1 + "</a></td>";
                    pageHtml += "<td class='page'><a href=\"javascript:void(0);\" onclick=\"PreviewsList.JumpPage(" + 2 + ");\" class=\"" + (PreviewsList.PageIndex == 2 ? "current" : "") + "\">" + 2 + "</a></td>";
                    pageHtml += "<td class='page'><a href=\"javascript:void(0);\" onclick=\"PreviewsList.JumpPage(" + 3 + ");\" class=\"" + (PreviewsList.PageIndex == 3 ? "current" : "") + "\">" + 3 + "</a></td>";
                }
                else {
                    pageHtml += "<td class='page'><a href=\"javascript:void(0);\" onclick=\"PreviewsList.JumpPage(" + 1 + ");\" class=\"" + (PreviewsList.PageIndex == 1 ? "current" : "") + "\">" + 1 + "</a></td>";
                    pageHtml += "<td class='page'><a href=\"javascript:void(0);\" onclick=\"PreviewsList.JumpPage(" + 2 + ");\" class=\"" + (PreviewsList.PageIndex == 2 ? "current" : "") + "\">" + 2 + "</a></td>";
                    if (PreviewsList.PageIndex == PreviewsList.PageCount) {
                        pageHtml += "<td class='page'><a href=\"javascript:void(0);\" onclick=\"PreviewsList.JumpPage(" + (startPage - 2) + ");\" class=\"more\">&#8230;</a></td>";
                    } else {
                        pageHtml += "<td class='page'><a href=\"javascript:void(0);\" onclick=\"PreviewsList.JumpPage(" + (startPage - 1) + ");\" class=\"more\">&#8230;</a></td>";
                    }

                }
            }
            if (PreviewsList.PageIndex == PreviewsList.PageCount) {
                pageHtml += "<td class='page'><a href=\"javascript:void(0);\" onclick=\"PreviewsList.JumpPage(" + (startPage - 1) + ");\" class=\"" + (PreviewsList.PageIndex == (startPage - 1) ? "current" : "") + "\">" + (startPage - 1) + "</a></td>";
            }
            for (PageCurrentNumber = startPage; PageCurrentNumber <= endPage; PageCurrentNumber++) {
                pageHtml += "<td class='page'><a href=\"javascript:void(0);\" onclick=\"PreviewsList.JumpPage(" + PageCurrentNumber + ");\" class=\"" + (PreviewsList.PageIndex == PageCurrentNumber ? "current" : "") + "\">" + PageCurrentNumber + "</a></td>";
            }

            if (PreviewsList.PageIndex == 1 && endPage <= PreviewsList.PageCount) {
                pageHtml += "<td class='page'><a href=\"javascript:void(0);\" onclick=\"PreviewsList.JumpPage(" + 4 + ");\" class=\"" + (PreviewsList.PageIndex == 4 ? "current" : "") + "\">" + 4 + "</a></td>";
                PageCurrentNumber = PageCurrentNumber + 1;
            }
            if (PreviewsList.PageIndex < PreviewsList.PageCount) {
                if (PageCurrentNumber < PreviewsList.PageCount - 2) {
                    pageHtml += "<td class='page'><a href=\"javascript:void(0);\" onclick=\"PreviewsList.JumpPage(" + PageCurrentNumber + ");\" class=\"more\">&#8230;</a>";
                    pageHtml += "<td class='page'><a href=\"javascript:void(0);\" onclick=\"PreviewsList.JumpPage(" + (PreviewsList.PageCount - 1) + ");\" class=\"" + (PreviewsList.PageIndex == (PreviewsList.PageCount - 1) ? "current" : "") + "\">" + (PreviewsList.PageCount - 1) + "</a></td>";
                    pageHtml += "<td class='page'><a href=\"javascript:void(0);\" onclick=\"PreviewsList.JumpPage(" + PreviewsList.PageCount + ");\" class=\"" + (PreviewsList.PageIndex == PreviewsList.PageCount ? "current" : "") + "\">" + PreviewsList.PageCount + "</a></td>";
                }
                else if (PageCurrentNumber == PreviewsList.PageCount) {
                    pageHtml += "<td class='page'><a href=\"javascript:void(0);\" onclick=\"PreviewsList.JumpPage(" + PreviewsList.PageCount + ");\" class=\"" + (PreviewsList.PageIndex == PreviewsList.PageCount ? "current" : "") + "\">" + PreviewsList.PageCount + "</a></td>";
                }
                else if (PageCurrentNumber == PreviewsList.PageCount - 1) {
                    pageHtml += "<td class='page'><a href=\"javascript:void(0);\" onclick=\"PreviewsList.JumpPage(" + (PreviewsList.PageCount - 1) + ");\" class=\"" + (PreviewsList.PageIndex == PreviewsList.PageCount - 1 ? "current" : "") + "\">" + (PreviewsList.PageCount - 1) + "</a></td>";
                    pageHtml += "<td class='page'><a href=\"javascript:void(0);\" onclick=\"PreviewsList.JumpPage(" + PreviewsList.PageCount + ");\" class=\"" + (PreviewsList.PageIndex == PreviewsList.PageCount ? "current" : "") + "\">" + PreviewsList.PageCount + "</a></td>";
                }
                else if (PageCurrentNumber == PreviewsList.PageCount - 2) {
                    pageHtml += "<td class='page'><a href=\"javascript:void(0);\" onclick=\"PreviewsList.JumpPage(" + (PreviewsList.PageCount - 2) + ");\" class=\"" + (PreviewsList.PageIndex == (PreviewsList.PageCount - 2) ? "current" : "") + "\">" + (PreviewsList.PageCount - 2) + "</a></td>";
                    pageHtml += "<td class='page'><a href=\"javascript:void(0);\" onclick=\"PreviewsList.JumpPage(" + (PreviewsList.PageCount - 1) + ");\" class=\"" + (PreviewsList.PageIndex == (PreviewsList.PageCount - 1) ? "current" : "") + "\">" + (PreviewsList.PageCount - 1) + "</a></td>";
                    pageHtml += "<td class='page'><a href=\"javascript:void(0);\" onclick=\"PreviewsList.JumpPage(" + PreviewsList.PageCount + ");\" class=\"" + (PreviewsList.PageIndex == PreviewsList.PageCount ? "current" : "") + "\">" + PreviewsList.PageCount + "</a></td>";
                }
            }
        }
        pageHtml += PreviewsList.PageCount == PreviewsList.PageIndex ? "" : ("<td class='page' style='width: 40px;'><a href='javascript:void(0);' onclick='" + nextPageClick + "'>下一页</a></td>");
        pageHtml += PreviewsList.PageIndex == PreviewsList.PageCount ? "" : ("<td class='page' style='width: 40px;'><a href='javascript:void(0);' onclick='PreviewsList.JumpPage(" + PreviewsList.PageCount + ");'>尾页</a></td>");
        $("#pager").html(pageHtml);
    },
    JumpPage: function (i) {
        var reg = /\d+/;
        if (!reg.test(i)) return;

        if (i < 1) this.PageIndex = 1;
        else if (i > this.PageCount && this.PageCount != -1) this.PageIndex = this.PageCount;
        else this.PageIndex = parseInt(i);
        this.GetData($(".concent a[class='current']").attr("id"));
    }
}